package cn.iocoder.yudao.module.system.service.tenant;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantCallChannelDO;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantCallChannelMapper;
import de.danielbechler.util.Collections;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Validated
public class TenantCallChannelServiceImpl implements TenantCallChannelService{

    @Resource
    private TenantCallChannelMapper tenantCallChannelMapper;
    @Override
    public void createTenantCallChannel(Long tenantId, List<Long> channelIdList, Boolean isClear) {
        if (isClear) {
            clearTenantCallChannel(tenantId);
        }
        if (Collections.isEmpty(channelIdList)) {
            return;
        }
        List<TenantCallChannelDO> list = channelIdList.stream().map(data -> new TenantCallChannelDO().setTenantId(tenantId).setChannelId(data)).collect(Collectors.toList());
        tenantCallChannelMapper.insertBatch(list);
    }

    @Override
    public void clearTenantCallChannel(Long tenantId) {
        tenantCallChannelMapper.delete(new LambdaQueryWrapperX<TenantCallChannelDO>().eqIfPresent(TenantCallChannelDO::getTenantId, tenantId));
    }

    @Override
    public List<Long> getCallChannelIdList(Long tenantId) {
        List<TenantCallChannelDO> channelDOS = getCallChannelList(tenantId);
        return channelDOS.stream().map(TenantCallChannelDO::getChannelId).collect(Collectors.toList());
    }

    @Override
    public List<TenantCallChannelDO> getCallChannelList(Long tenantId) {
        return tenantCallChannelMapper.selectList(TenantCallChannelDO::getTenantId, tenantId);
    }
}
